#version 150

#moj_import <fog.glsl>
#moj_import <noise.glsl>
#moj_import <vsh_util.glsl>

uniform vec4 ColorModulator;
uniform sampler2D Sampler0;
uniform float GameTime;
uniform float FogStart;
uniform float FogEnd;
uniform vec4 FogColor;

in float vertexDistance;
in vec2 texCoord0;
in vec4 vertexColor;
in vec3 viewPos;
in mat4 aProjMat;

out vec4 fragColor;

void main() {
    float height = viewPos.y - vertexDistance/8.0 + 16.0*perlin_noise((viewPos.xz + vec2(GameTime*12800.0))/16.0);
    float strength;
    if(height> 0.0){
        strength = 0.0;
    }else if(height < -128.0){
        strength = 1.0;
    }else{
        strength = height / -128.0;
    }

    vec4 color= OBJ_COLOR;
    if(color.a < 0.1)
        discard;
    fragColor = mix(color, FOG_COLOR, strength);

    if(isGUI(aProjMat)){
        color = texture(Sampler0, texCoord0) * vertexColor * ColorModulator;
        if (color.a < 0.1) {
            discard;
        }
        fragColor = linear_fog(color, vertexDistance, FogStart, FogEnd, FogColor);
    }
}
